package com.aiyige.page.publish.coursesingleprice;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.InputSinglePricePage)
public class InputSinglePricePage extends AppCompatActivity {
    public static final String EXTRA_KEY_PRICE = "com.aiyige.extra.EXTRA_KEY_PRICE";

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.titleActionBtn)
    Button titleActionBtn;
    @BindView(R.id.priceEt)
    EditText priceEt;
    @Autowired
    String price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_input_single_price);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.set_single_course_price);
        titleActionBtn.setText(R.string.finish);
        priceEt.setText(price);
    }

    @OnClick({R.id.titleBackBtn, R.id.titleActionBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
            case R.id.titleActionBtn:
                try {
                    double value = Double.parseDouble(priceEt.getText().toString());
                    Intent data = new Intent();
                    data.putExtra(EXTRA_KEY_PRICE, StringUtils.priceFormat(value));
                    setResult(RESULT_OK, data);
                    finish();
                } catch (Exception e) {
                    ToastX.show(R.string.invalid_price);
                    priceEt.setText("");
                }
                break;
        }
    }
}
